package utwente.hmi.touchmol.elements;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.util.LinkedList;
import java.util.ListIterator;

import javax.swing.Box;
import javax.swing.BoxLayout;

import utwente.hmi.touchmol.TouchMolGUI;

/**
 * Moderator panel, used in ModeratorGUI and QueueModeratorGUI.
 * @author Jeroen Logtenberg
 */
public class ModeratorPanel extends ButtonPanel {
	
	private static final long serialVersionUID = 1L;
	
	private Dimension currentSize;
	private int moderator;
	private LinkedList<UserButton> buttons; 
	
	/**
	 * Makes a new moderator panel, a panel with userbuttons red, blue, green and yellow.
	 * @param width Width in pixels
	 * @param height Height in pixels
	 * @param moderator The user allowed to operate the panel. Use TouchMolGUI.USER_[COLOR].
	 */
	public ModeratorPanel(int width, int height, int moderator){
		buttons = new LinkedList<UserButton>();
		UserButton red = UserButton.getRedButton();
		UserButton blue = UserButton.getBlueButton();
		UserButton green = UserButton.getGreenButton();
		UserButton yellow = UserButton.getYellowButton();

		buttons.add(red);
		buttons.add(blue);
		buttons.add(green);
		buttons.add(yellow);
		
		this.moderator = moderator;
		
		currentSize = new Dimension(width, height);
		setSize(currentSize);
		setPreferredSize(currentSize);
		setBackground(Color.BLACK);

		setVisible(true);
		BoxLayout box = new BoxLayout(this,BoxLayout.Y_AXIS);
		setLayout(box);
		
		int h = UserButton.defaultHeight;

		//Two ridid areas: both half the size of the space remaining after
		//the doodles have been placed. 4 doodles, 3 spaces between them (= h * 7)
		add(Box.createRigidArea(new Dimension(0, (height - (h * 7)) / 2)));
		add(blue);
		add(Box.createVerticalGlue());
		add(red);
		add(Box.createVerticalGlue());
		add(green);
		add(Box.createVerticalGlue());
		add(yellow);
		add(Box.createRigidArea(new Dimension(0, (height - (h * 7)) / 2)));
		
		takeTurn(blue);
	}

	public int getModerator(){
		return moderator;
	}
	
	@Override
	public UserButton buttonTouched(Point location) {
		ListIterator<UserButton> it = buttons.listIterator();
		UserButton b;
		TouchMolGUI.debug("Checking for touch on " + location);
		while(it.hasNext()){
			b = it.next();
			if(b.buttonTouched(location)) {
				return b;
			}
		}
		return null;
	}

	@Override
	public void releaseTurn(UserButton user) {
		TouchMolGUI.debug("Releasing " + user.getUserID());
		ListIterator<UserButton> it = buttons.listIterator();
		UserButton b;
		while(it.hasNext()){
			b = it.next();
			if(b.getUserID() == user.getUserID())
				b.setBorder(null);
		}
		
	}
	
	@Override
	public void requestTurn(UserButton user) {
		//Moderator cannot request turn.
		
	}

	@Override
	public void takeTurn(UserButton user) {
		ListIterator<UserButton> it = buttons.listIterator();
		UserButton b;
		while(it.hasNext()){
			b = it.next();
			if(b.getUserID() == user.getUserID())
				b.setBorder(UserButton.BORDER_TURN);
		}
	}

}
